package com.lzy.crm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.mybatis.Example;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.dao.CrowdMapper;
import com.lzy.crm.dao.CrowdUserMapper;
import com.lzy.crm.dao.UserMapper;
import com.lzy.crm.dao.criteria.CrowdCriteria;
import com.lzy.crm.domain.Crowd;
import com.lzy.crm.domain.CrowdUser;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.CrowdService;
import com.lzy.crm.service.UserService;
import com.lzy.crm.util.UserConstants;
import com.lzy.crm.vo.PlayerInfo;
import com.lzy.crm.wechat.consts.CrowdConstants;


/**
 * 牌友圈功能
 * @author Administrator
 *
 */
@Service
public class CrowdServiceImpl extends BaseServiceImpl<Crowd, CrowdCriteria> implements CrowdService{

	
	@Resource
	private CrowdMapper crowdMapper;
	
	@Resource
	private CrowdUserMapper crowdUserMapper;
	
	@Resource
	private UserMapper  userMapper;
	
	@Resource
	private UserService userService;
	
	
	@Override
	public BaseMapper<Crowd, CrowdCriteria> getBaseMapper() {
		return crowdMapper;
	}

	/**
	 * 获取自己的牌友圈信息
	 */
	@Override
	public List<Crowd> getMyCrowdListByUnionId(String unionId){
		Example<CrowdCriteria> example = new Example<CrowdCriteria>(CrowdCriteria.class);
		CrowdCriteria crowdCriteria = example.createCriteria();
		crowdCriteria.andStatusEqualTo(CrowdConstants.crowd_status);
		crowdCriteria.andUnionidEqualTo(unionId);
		return crowdMapper.getListByExample(example);
		
	}
	

	
	
	/**
	 * 获取自己加入的牌友圈信息
	 * @param unionId
	 * @return
	 */
	@Override
	public List<Crowd> getJoinCrowdListByUnionId(String unionId){
		return crowdMapper.getJoinCrowdListByUnionId(unionId);
	}

	
	/**
	 * 获取自己加入的牌友圈信息
	 * @param unionId
	 * @return
	 */
	@Override
	public List<Crowd> getJoinCrowdListByUnionIdbyPage(String unionId,PageQuery pageQuery){
		return crowdMapper.getJoinCrowdListByUnionIdbyPage(unionId,pageQuery);
	}
	
	/**
	 * 创建牌友圈
	 * @param crowdName
	 * @param remark
	 * @param user
	 */
	public Integer addCrowd(String crowdName,String remark, PpUser user)throws Exception{
		
		if(user==null){
			return 2;
		}
		
		/*if(StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_AGENT)||
				StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
			if(crowdList.size()>=CrowdConstants.nub_agent){
				return 3;
			}
		}else{
			return 2;
		}*/
		
		Integer crowdId =crowdMapper.getMapCrowdId();
		if(crowdId==null||crowdId==0){
			crowdId=999;
		}
		crowdId =crowdId+1;
		Crowd crowd = new Crowd();
		crowd.setId(crowdId);
		crowd.setCrowdName(crowdName);
		crowd.setOutUserName(user.getNickname());
		crowd.setUnionid(user.getUnionid());
		crowd.setGameId(user.getUid());
		crowd.setIsCard("0");
		crowd.setGameRule(0);
		crowd.setStatus("1");
		crowd.setNub(1);
		crowd.setRemark(remark);
		crowd.setCreateTime(new java.util.Date());
		crowdMapper.insertSelective(crowd);
		
		CrowdUser crowdUser = new CrowdUser();
		crowdUser.setCrowdId(crowdId);
		crowdUser.setGameId(user.getUid());
		crowdUser.setUnionid(user.getUnionid());
		crowdUser.setNickName(user.getNickname());
		crowdUser.setOutUserName(user.getNickname());
		crowdUser.setIsCard("1");
		crowdUser.setUseNub(0);
		crowdUser.setUseNubTotal(0);
		crowdUser.setRemark("群主");
		crowdUser.setIsManager(CrowdConstants.crowd_is_manage_create);
		crowdUser.setCreateTime(new java.util.Date());
		crowdUserMapper.insertSelective(crowdUser);
		
		return 1;
	}
	
	/**
	 * 创建牌友圈
	 * @param crowdName
	 * @param remark
	 * @param user
	 */
	public Integer addCrowdByMessage(String crowdName,String remark, PlayerInfo playerInfo)throws Exception{
		
		Integer crowdId =crowdMapper.getMapCrowdId();
		if(crowdId==null||crowdId==0){
			crowdId=999;
		}
		crowdId =crowdId+1;
		Crowd crowd = new Crowd();
		crowd.setId(crowdId);
		crowd.setCrowdName(crowdName);
		crowd.setOutUserName(playerInfo.getNickName());
		crowd.setUnionid(playerInfo.getUnionid());
		crowd.setGameId(playerInfo.getGameId());
		crowd.setIsCard("1");
		crowd.setStatus("1");
		crowd.setNub(1);
		crowd.setRemark(remark);
		crowd.setCreateTime(new java.util.Date());
		crowdMapper.insertSelective(crowd);
		
		List<CrowdUser> crowdUseList = crowdUserMapper.getCrowdUserListByGameId(playerInfo.getGameId().toString());
		// 是否设置默认牌圈
		boolean isCard =false;
		for(CrowdUser crowdUserbf:crowdUseList){
			if(StringUtil.equals("1", crowdUserbf.getIsCard())){
				isCard = true;
			}
		}
		CrowdUser crowdUser = new CrowdUser();
		crowdUser.setCrowdId(crowd.getId());
		crowdUser.setGameId(playerInfo.getGameId());
		crowdUser.setUnionid(playerInfo.getUnionid());
		crowdUser.setNickName(playerInfo.getNickName());
		crowdUser.setOutUserName(playerInfo.getNickName());
		crowdUser.setIsCard(isCard?"0":"1");
		crowdUser.setUseNub(0);
		crowdUser.setUseNubTotal(0);
		crowdUser.setRemark("群主");
		crowdUser.setCreateTime(new java.util.Date());
		crowdUserMapper.insertSelective(crowdUser);
		return 1;
	}
	
	
	
}
